clear all

#delimit ;
*  PSID DATA CENTER *****************************************************
   JOBID            : 318310                            
   DATA_DOMAIN      : IND                               
   USER_WHERE       : NULL                              
   FILE_TYPE        : All Individuals Data              
   OUTPUT_DATA_TYPE : ASCII                             
   STATEMENTS       : do                                
   CODEBOOK_TYPE    : PDF                               
   N_OF_VARIABLES   : 154                               
   N_OF_OBSERVATIONS: 75326                             
   MAX_REC_LENGTH   : 316                               
   DATE & TIME      : March 28, 2023 @ 22:01:47
*************************************************************************
;

infix
      ER30000              1 - 1           ER30001              2 - 5           ER30002              6 - 8     
      V4301                9 - 9           V5055               10 - 10          V5100               11 - 11    
      ER30188             12 - 15          ER30189             16 - 17          ER30190             18 - 18    
      V8801               19 - 19          V9382               20 - 20          ER30399             21 - 24    
      ER30400             25 - 26          ER30401             27 - 28          V10001              29 - 29    
      V11029              30 - 30          ER30429             31 - 34          ER30430             35 - 36    
      ER30431             37 - 38          V11101              39 - 39          V12383              40 - 40    
      V12387              41 - 41          ER30463             42 - 45          ER30464             46 - 47    
      ER30465             48 - 49          V12501              50 - 50          V13635              51 - 51    
      ER30498             52 - 55          ER30499             56 - 57          ER30500             58 - 59    
      V13701              60 - 60          V14682              61 - 61          ER30535             62 - 65    
      ER30536             66 - 67          ER30537             68 - 69          V14801              70 - 70    
      V16156              71 - 71          ER30570             72 - 75          ER30571             76 - 77    
      ER30572             78 - 79          V16301              80 - 80          V17542              81 - 81    
      ER30606             82 - 85          ER30607             86 - 87          ER30608             88 - 89    
      V17701              90 - 90          V18893              91 - 91          ER30642             92 - 96    
      ER30643             97 - 98          ER30644             99 - 100         V19001             101 - 101   
      V20193             102 - 102         ER30689            103 - 106         ER30690            107 - 108   
      ER30691            109 - 110         V20301             111 - 111         V21499             112 - 112   
      ER30733            113 - 116         ER30734            117 - 118         ER30735            119 - 120   
      V21601             121 - 121         V23331             122 - 122         ER30806            123 - 127   
      ER30807            128 - 129         ER30808            130 - 131         ER2001             132 - 132   
      ER4157C            133 - 133         ER33101            134 - 138         ER33102            139 - 140   
      ER33103            141 - 142         ER5001             143 - 143         ER6997C            144 - 144   
      ER33201            145 - 149         ER33202            150 - 151         ER33203            152 - 153   
      ER7001             154 - 154         ER9248C            155 - 155         ER33301            156 - 159   
      ER33302            160 - 161         ER33303            162 - 163         ER10001            164 - 164   
      ER12221C           165 - 165         ER33401            166 - 170         ER33402            171 - 172   
      ER33403            173 - 174         ER13001            175 - 175         ER16431A           176 - 176   
      ER33501            177 - 181         ER33502            182 - 183         ER33503            184 - 185   
      ER17001            186 - 186         ER20377A           187 - 187         ER33601            188 - 191   
      ER33602            192 - 193         ER33603            194 - 195         ER21001            196 - 196   
      ER24146            197 - 197         ER33701            198 - 202         ER33702            203 - 204   
      ER33703            205 - 206         ER25001            207 - 207         ER28045            208 - 208   
      ER33801            209 - 213         ER33802            214 - 215         ER33803            216 - 217   
      ER36001            218 - 218         ER41035            219 - 219         ER33901            220 - 224   
      ER33902            225 - 226         ER33903            227 - 228         ER42001            229 - 229   
      ER46977            230 - 230         ER46979            231 - 231         ER34001            232 - 236   
      ER34002            237 - 238         ER34003            239 - 240         ER47301            241 - 241   
      ER52401            242 - 242         ER52403            243 - 243         ER34101            244 - 248   
      ER34102            249 - 250         ER34103            251 - 252         ER53001            253 - 253   
      ER57541            254 - 255         ER57651            256 - 257         ER58219            258 - 258   
      ER58221            259 - 259         ER34201            260 - 264         ER34202            265 - 266   
      ER34203            267 - 268         ER60001            269 - 269         ER64663            270 - 271   
      ER64802            272 - 273         ER65455            274 - 274         ER65457            275 - 275   
      ER34301            276 - 280         ER34302            281 - 282         ER34303            283 - 284   
      ER66001            285 - 285         ER70736            286 - 287         ER70874            288 - 289   
      ER71534            290 - 290         ER71536            291 - 291         ER34501            292 - 296   
      ER34502            297 - 298         ER34503            299 - 300         ER72001            301 - 301   
      ER76744            302 - 303         ER76889            304 - 305         ER77595            306 - 306   
      ER77597            307 - 307         ER34701            308 - 312         ER34702            313 - 314   
      ER34703            315 - 316   
using ./RawData/geo_headspouse.txt, clear 
;
label variable ER30000         "RELEASE NUMBER"                           ;
label variable ER30001         "1968 INTERVIEW NUMBER"                    ;
label variable ER30002         "PERSON NUMBER                         68" ;
label variable V4301           "RELEASE NUMBER"                           ;
label variable V5055           "REGION HD GREW UP"                        ;
label variable V5100           "REGION WF GREW UP"                        ;
label variable ER30188         "1976 INTERVIEW NUMBER"                    ;
label variable ER30189         "SEQUENCE NUMBER                       76" ;
label variable ER30190         "RELATIONSHIP TO HEAD                  76" ;
label variable V8801           "RELEASE NUMBER"                           ;
label variable V9382           "REGION 83 HD GREW UP"                     ;
label variable ER30399         "1983 INTERVIEW NUMBER"                    ;
label variable ER30400         "SEQUENCE NUMBER                       83" ;
label variable ER30401         "RELATIONSHIP TO HEAD                  83" ;
label variable V10001          "RELEASE NUMBER"                           ;
label variable V11029          "REGION 84 HD GREW UP"                     ;
label variable ER30429         "1984 INTERVIEW NUMBER"                    ;
label variable ER30430         "SEQUENCE NUMBER                       84" ;
label variable ER30431         "RELATIONSHIP TO HEAD                  84" ;
label variable V11101          "RELEASE NUMBER"                           ;
label variable V12383          "REGION 85 HD GREW UP"                     ;
label variable V12387          "REGION 85 WF GREW UP"                     ;
label variable ER30463         "1985 INTERVIEW NUMBER"                    ;
label variable ER30464         "SEQUENCE NUMBER                       85" ;
label variable ER30465         "RELATIONSHIP TO HEAD                  85" ;
label variable V12501          "RELEASE NUMBER"                           ;
label variable V13635          "REGION 86 HD GREW UP"                     ;
label variable ER30498         "1986 INTERVIEW NUMBER"                    ;
label variable ER30499         "SEQUENCE NUMBER                       86" ;
label variable ER30500         "RELATIONSHIP TO HEAD                  86" ;
label variable V13701          "RELEASE NUMBER"                           ;
label variable V14682          "REGION 87 HD GREW UP"                     ;
label variable ER30535         "1987 INTERVIEW NUMBER"                    ;
label variable ER30536         "SEQUENCE NUMBER                       87" ;
label variable ER30537         "RELATIONSHIP TO HEAD                  87" ;
label variable V14801          "RELEASE NUMBER"                           ;
label variable V16156          "REGION 88 HD GREW UP"                     ;
label variable ER30570         "1988 INTERVIEW NUMBER"                    ;
label variable ER30571         "SEQUENCE NUMBER                       88" ;
label variable ER30572         "RELATION TO HEAD                      88" ;
label variable V16301          "RELEASE NUMBER"                           ;
label variable V17542          "REGION 89 HD GREW UP"                     ;
label variable ER30606         "1989 INTERVIEW NUMBER"                    ;
label variable ER30607         "SEQUENCE NUMBER                       89" ;
label variable ER30608         "RELATION TO HEAD                      89" ;
label variable V17701          "RELEASE NUMBER"                           ;
label variable V18893          "REGION 90 HD GREW UP"                     ;
label variable ER30642         "1990 INTERVIEW NUMBER"                    ;
label variable ER30643         "SEQUENCE NUMBER                       90" ;
label variable ER30644         "RELATION TO HEAD                      90" ;
label variable V19001          "RELEASE NUMBER"                           ;
label variable V20193          "REGION 91 HD GREW UP"                     ;
label variable ER30689         "1991 INTERVIEW NUMBER"                    ;
label variable ER30690         "SEQUENCE NUMBER                       91" ;
label variable ER30691         "RELATION TO HEAD                      91" ;
label variable V20301          "RELEASE NUMBER"                           ;
label variable V21499          "REGION 92 HD GREW UP"                     ;
label variable ER30733         "1992 INTERVIEW NUMBER"                    ;
label variable ER30734         "SEQUENCE NUMBER                       92" ;
label variable ER30735         "RELATION TO HEAD                      92" ;
label variable V21601          "RELEASE NUMBER"                           ;
label variable V23331          "REGION 1993 HD GREW UP"                   ;
label variable ER30806         "1993 INTERVIEW NUMBER"                    ;
label variable ER30807         "SEQUENCE NUMBER                       93" ;
label variable ER30808         "RELATION TO HEAD                      93" ;
label variable ER2001          "RELEASE NUMBER"                           ;
label variable ER4157C         "REGION HD GREW UP"                        ;
label variable ER33101         "1994 INTERVIEW NUMBER"                    ;
label variable ER33102         "SEQUENCE NUMBER                       94" ;
label variable ER33103         "RELATION TO HEAD                      94" ;
label variable ER5001          "RELEASE NUMBER"                           ;
label variable ER6997C         "REGION HD GREW UP"                        ;
label variable ER33201         "1995 INTERVIEW NUMBER"                    ;
label variable ER33202         "SEQUENCE NUMBER                       95" ;
label variable ER33203         "RELATION TO HEAD                      95" ;
label variable ER7001          "RELEASE NUMBER"                           ;
label variable ER9248C         "REGION HD GREW UP"                        ;
label variable ER33301         "1996 INTERVIEW NUMBER"                    ;
label variable ER33302         "SEQUENCE NUMBER                       96" ;
label variable ER33303         "RELATION TO HEAD                      96" ;
label variable ER10001         "RELEASE NUMBER"                           ;
label variable ER12221C        "REGION HD GREW UP"                        ;
label variable ER33401         "1997 INTERVIEW NUMBER"                    ;
label variable ER33402         "SEQUENCE NUMBER                       97" ;
label variable ER33403         "RELATION TO HEAD                      97" ;
label variable ER13001         "RELEASE NUMBER"                           ;
label variable ER16431A        "REGION HD GREW UP"                        ;
label variable ER33501         "1999 INTERVIEW NUMBER"                    ;
label variable ER33502         "SEQUENCE NUMBER                       99" ;
label variable ER33503         "RELATION TO HEAD                      99" ;
label variable ER17001         "RELEASE NUMBER"                           ;
label variable ER20377A        "REGION HD GREW UP"                        ;
label variable ER33601         "2001 INTERVIEW NUMBER"                    ;
label variable ER33602         "SEQUENCE NUMBER                       01" ;
label variable ER33603         "RELATION TO HEAD                      01" ;
label variable ER21001         "RELEASE NUMBER"                           ;
label variable ER24146         "REGION HD GREW UP"                        ;
label variable ER33701         "2003 INTERVIEW NUMBER"                    ;
label variable ER33702         "SEQUENCE NUMBER                       03" ;
label variable ER33703         "RELATION TO HEAD                      03" ;
label variable ER25001         "RELEASE NUMBER"                           ;
label variable ER28045         "REGION HD GREW UP"                        ;
label variable ER33801         "2005 INTERVIEW NUMBER"                    ;
label variable ER33802         "SEQUENCE NUMBER                       05" ;
label variable ER33803         "RELATION TO HEAD                      05" ;
label variable ER36001         "RELEASE NUMBER"                           ;
label variable ER41035         "REGION HD GREW UP"                        ;
label variable ER33901         "2007 INTERVIEW NUMBER"                    ;
label variable ER33902         "SEQUENCE NUMBER                       07" ;
label variable ER33903         "RELATION TO HEAD                      07" ;
label variable ER42001         "RELEASE NUMBER"                           ;
label variable ER46977         "REGION HD GREW UP"                        ;
label variable ER46979         "REGION WF GREW UP"                        ;
label variable ER34001         "2009 INTERVIEW NUMBER"                    ;
label variable ER34002         "SEQUENCE NUMBER                       09" ;
label variable ER34003         "RELATION TO HEAD                      09" ;
label variable ER47301         "RELEASE NUMBER"                           ;
label variable ER52401         "REGION HD GREW UP"                        ;
label variable ER52403         "REGION WF GREW UP"                        ;
label variable ER34101         "2011 INTERVIEW NUMBER"                    ;
label variable ER34102         "SEQUENCE NUMBER                       11" ;
label variable ER34103         "RELATION TO HEAD                      11" ;
label variable ER53001         "RELEASE NUMBER"                           ;
label variable ER57541         "K33 STATE WIFE WAS BORN"                  ;
label variable ER57651         "L33 STATE HEAD WAS BORN"                  ;
label variable ER58219         "REGION HD GREW UP"                        ;
label variable ER58221         "REGION WF GREW UP"                        ;
label variable ER34201         "2013 INTERVIEW NUMBER"                    ;
label variable ER34202         "SEQUENCE NUMBER                       13" ;
label variable ER34203         "RELATION TO HEAD                      13" ;
label variable ER60001         "RELEASE NUMBER"                           ;
label variable ER64663         "K33 STATE SPOUSE WAS BORN"                ;
label variable ER64802         "L33 STATE HEAD WAS BORN"                  ;
label variable ER65455         "REGION HD GREW UP"                        ;
label variable ER65457         "REGION SP GREW UP"                        ;
label variable ER34301         "2015 INTERVIEW NUMBER"                    ;
label variable ER34302         "SEQUENCE NUMBER                       15" ;
label variable ER34303         "RELATION TO HEAD                      15" ;
label variable ER66001         "RELEASE NUMBER"                           ;
label variable ER70736         "K33 STATE SPOUSE WAS BORN"                ;
label variable ER70874         "L33 STATE REFERENCE PERSON WAS BORN"      ;
label variable ER71534         "REGION REFERENCE PERSON GREW UP"          ;
label variable ER71536         "REGION SP GREW UP"                        ;
label variable ER34501         "2017 INTERVIEW NUMBER"                    ;
label variable ER34502         "SEQUENCE NUMBER                       17" ;
label variable ER34503         "RELATION TO REFERENCE PERSON          17" ;
label variable ER72001         "RELEASE NUMBER"                           ;
label variable ER76744         "K33 STATE SPOUSE WAS BORN"                ;
label variable ER76889         "L33 STATE REFERENCE PERSON WAS BORN"      ;
label variable ER77595         "REGION REFERENCE PERSON GREW UP"          ;
label variable ER77597         "REGION SP GREW UP"                        ;
label variable ER34701         "2019 INTERVIEW NUMBER"                    ;
label variable ER34702         "SEQUENCE NUMBER                       19" ;
label variable ER34703         "RELATION TO REFERENCE PERSON          19" ;

#delimit cr

***************************************************************
do ./RawData/geo_headspouse_formats.do //PSID-provided cleaning
***************************************************************

// CREATE SON_ID
   ren ER30001 famid
   ren ER30002 personnumber

   gen son_id = famid*1000 + personnumber
   label var son_id "Child 1968 ID"
   duplicates report son_id //no dups
         
//RENAME "REGION GREW UP" AND "STATE GREW UP" VARS

   ren V5055 reggrewup_HD_1976
   ren V5100 reggrewup_SP_1976

   ren V9382 reggrewup_HD_1983
   ren V11029 reggrewup_HD_1984

   ren V12383 reggrewup_HD_1985
   ren V12387 reggrewup_SP_1985
   
   ren V13635 reggrewup_HD_1986
   ren V14682 reggrewup_HD_1987
   ren V16156 reggrewup_HD_1988
   ren V17542 reggrewup_HD_1989
   ren V18893 reggrewup_HD_1990
   ren V20193 reggrewup_HD_1991
   ren V21499 reggrewup_HD_1992
   ren V23331 reggrewup_HD_1993
   ren ER4157C reggrewup_HD_1994
   ren ER6997C reggrewup_HD_1995
   ren ER9248C reggrewup_HD_1996

   ren ER12221C reggrewup_HD_1997
   ren ER16431A reggrewup_HD_1999
   ren ER20377A reggrewup_HD_2001
   ren ER24146 reggrewup_HD_2003
   ren ER28045 reggrewup_HD_2005
   ren ER41035 reggrewup_HD_2007

   ren ER46977 reggrewup_HD_2009
   ren ER46979 reggrewup_SP_2009

   ren ER52401 reggrewup_HD_2011
   ren ER52403 reggrewup_SP_2011

   ren ER58219 reggrewup_HD_2013
   ren ER58221 reggrewup_SP_2013
   ren ER57651 state_born_HD_2013
   ren ER57541 state_born_SP_2013

   ren ER65455 reggrewup_HD_2015
   ren ER65457 reggrewup_SP_2015
   ren ER64802 state_born_HD_2015
   ren ER64663 state_born_SP_2015

   ren ER71534 reggrewup_HD_2017
   ren ER71536 reggrewup_SP_2017
   ren ER70874 state_born_HD_2017
   ren ER70736 state_born_SP_2017

   ren ER77595 reggrewup_HD_2019
   ren ER77597 reggrewup_SP_2019  
   ren ER76889 state_born_HD_2019
   ren ER76744 state_born_SP_2019

//DROP OTHER UNNECESSARY VARIABLES
lookfor relat
local relate "`r(varlist)'"

   foreach v of local relate {
       drop `v'
   }

lookfor rele
local release "`r(varlist)'"

   foreach v of local release {
       drop `v'
   }

lookfor number 
local number "`r(varlist)'"

   foreach v of local number {
      drop `v'
   }

order son_id
sort son_id
compress 

save ./RawData/geo_headspouse, replace
